package cn.xjy.ddlc.context;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import cn.xjy.ddlc.model.Column;

/**
 * 匹配好的列
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
@Slf4j
public class MatchedColumnItem extends AbstractMatchedItem {
    private Column left;
    
    private Column right;
    
    private CompareResult result;
    
    @Override
    public CompareResult compare() {
        if (left == null) {
            result = CompareResult.LEFT_NOT_EXIST;
            return result;
        }
        if (right == null) {
            result = CompareResult.RIGHT_NOT_EXIST;
            return result;
        }
        result = CompareResult.NOT_EQUAL;
        if (StringUtils.equals(left.getType(), right.getType())
                && StringUtils.equals(left.getComment(), right.getComment())
                && StringUtils.equals(left.getDefaultValue(), right.getDefaultValue())
                && equals(left.getLength(), right.getLength()) && left.getScale() == right.getScale()) {
            result = CompareResult.EQUAL;
        } else {
            log.info("left:{}", left);
            log.info("left:{}", right);
        }
        return result;
    }
    
    private boolean equals(String left, String right) {
        if (left == null && left == null) {
            return true;
        }
        
        return left != null && left.equals(right);
    }
    
}
